<?php
	
	/**
	 * 转帐
	 * @param int $pTransferType;//转账类型 1确认转账 2 取消转账
	 * @param int $deal_id  标的id	 
	 * @param string $ref_data 逗号分割的, 1：投资,填还款日期(int)  ; 2代偿，3代偿还款列表; 4债权转让: id; 5结算担保收益:金额，如果为0,则取fanwe_deal.guarantor_pro_fit_amt ;
	 * @param int $MerCode  商户ID
	 * @param string $cert_md5 
	 * @param string $post_url
	 * @return string
	 */
	function FreezeToTransfer($user_id,$tuser_id,$pTrdAmt,$platformNo,$post_url,$contract_no,$mchnt_name=0){
		$results = array();
		$results['status'] = 1;
		if($mchnt_name==1){
			$in_cust_no = $user_id;
			$rem = '佣金';
		}else{
			$user_id = intval($user_id);
			$user = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where id = ".$user_id);
			$in_cust_no = $user['mobile'];
			$rem = '';
		}
		
		$t_user_id = intval($tuser_id);	
		$tuser = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where id = ".$t_user_id);

		$Sign_data= array();
		$Sign_data['ver']="0.44";
		$Sign_data['contract_no']=$contract_no; 
		$Sign_data['in_cust_no']=$in_cust_no; 
		$Sign_data['mchnt_cd']=$platformNo; 
		$Sign_data['mchnt_txn_ssn']=$tuser['id'].time();
		$Sign_data['out_cust_no']=$tuser["mobile"];
		$Sign_data['amt']=$pTrdAmt*100;
		$Sign_data['rem']=$rem;   
			ksort($Sign_data);
			$Sign_old = '';
			foreach($Sign_data as $k => $v){
				if($k == 'ver'){
					$Sign_old .=$v;
					$Sign_old_a .=$k;
				}else{
					$Sign_old .= $v."|";
					$Sign_old_a .= $k."+|+";
				}
			}
			/* 签名数据 */
			$pSign=rsaSign($Sign_old,PRIVATE_KEY_PATH);
			/* 请求参数 */  
			$req = http_build_query($Sign_data)."&signature=".urlencode($pSign);

			$fuioupay_log = array();
			$fuioupay_log['code'] = 'Pay_Transfer';
			$fuioupay_log['create_date'] = to_date(TIME_UTC,'Y-m-d H:i:s');
			$fuioupay_log['strxml'] = $req;
			$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log);
			$log_id = $GLOBALS['db']->insert_id();

			/* 调用账户查询服务 */
			$options = array(
				CURLOPT_POST => TRUE,
				CURLOPT_RETURNTRANSFER => TRUE,
				CURLOPT_SSL_VERIFYPEER=>0,
				CURLOPT_SSL_VERIFYHOST=>0,
				CURLOPT_POSTFIELDS =>$req,
			
			); 
		    $ch = curl_init($post_url); 
		    curl_setopt_array($ch,$options); 
		   	$resultStr = curl_exec($ch);
		    curl_close($ch); 
		
			$result = array();
 			if (empty($ch)){
				$result = array();
				$result['resp_code'] = 9999;
				$result['pErrMsg'] = '返回出错';
				print_r(htmlentities($result));echo "<hr/>";exit;
			}else{
				require_once APP_ROOT_PATH.'system/collocation/fuioupay/xml.php';
				$str3ParaInfo = @XML_unserialize($resultStr);
				$str3Req = $str3ParaInfo['ap']['plain'];
				$result = array();
				$result['resp_code'] = $str3Req["resp_code"];
				$result['mchnt_cd'] = $str3Req["mchnt_cd"];
				$result['mchnt_txn_ssn'] = $str3Req["mchnt_txn_ssn"];
				$result['signature'] =  $str3ParaInfo['ap']['signature'];

				if($str3Req["resp_code"] =='0000')
				{
					$fuiou_code = fuiou_code();
					$str =  "resp_code:".$fuiou_code[$result['resp_code']];
					$error_data = array('html'=>$str);
					$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$error_data,'UPDATE',"id = ".$log_id);
				}else{
					$fuiou_code = fuiou_code();
					$str =  "resp_code:".$fuiou_code[$result['resp_code']].",pErrMsg:失败";
					$error_data = array('html'=>$str);
					$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$error_data,'UPDATE',"id = ".$log_id);
				}
			
			}
		
				
		return $results;
	}
	
?>